package cn.highedu.coolshark.product.controller;

import cn.highedu.coolshark.common.JsonResult;
import cn.highedu.coolshark.product.service.IUploadService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器类
 */
@RestController
@Slf4j
@Api(tags = "文件上传模块")
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private IUploadService uploadService;

    @PostMapping("/brand-logo")
    @ApiOperation("上传Logo文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "logo", value = "品牌Logo", required = true, dataType = "File")
    })
    @ApiResponse(code = 20000, message = "响应成功", response = String.class)
    public JsonResult<String> uploadBrandLogo(MultipartFile logo) {
        log.debug("开始处理【上传logo文件】的请求, 参数: {}", logo);
        String fileUrl = uploadService.uploadBrandLogo(logo);
        return JsonResult.ok(fileUrl);
    }

    @PostMapping("/product-image")
    @ApiOperation("上传商品图片")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "image", value = "商品图片", required = true, dataType = "File")
    })
    @ApiResponse(code = 20000, message = "响应成功", response = String.class)
    public JsonResult<String> uploadProductImage(MultipartFile image) {
        log.debug("开始处理【上传商品图片】的请求, 参数: {}", image);
        String fileUrl = uploadService.uploadProductImage(image);
        return JsonResult.ok(fileUrl);
    }
    @PostMapping("/delete")
    @ApiOperation("删除文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "fileUrl", value = "文件的URL", required = true, dataType = "String")
    })
    public JsonResult<Void> delete(@RequestBody String fileUrl) {
        log.debug("开始处理【删除图片】的请求, 参数: {}", fileUrl);
        uploadService.delete(fileUrl);
        return JsonResult.ok();
    }

    @PostMapping("/admin-avatar")
    @ApiOperation("上传管理员头像")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "avatar", value = "管理员头像", required = true, dataType = "File")
    })
    @ApiResponse(code = 20000, message = "响应成功", response = String.class)
    public JsonResult<String> uploadAdminAvatar(
            @RequestParam MultipartFile avatar,
            @RequestParam String title) {
        log.debug("开始处理【上传管理员头像】的请求, 参数: {}, {}", avatar, title);
        String fileUrl = uploadService.uploadAdminAvatar(avatar);
        return JsonResult.ok(fileUrl);
    }
}
